<template>
  <div>
    <el-card>
      <el-form ref="form" :model="form" :inline="true" :rules="rules">
        <h3>系统登录</h3>

        <el-form-item label="账号" prop="username">
          <el-input placeholder="请输入账号" v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!-- 当使用:inline="true"开启同一行的模式时，使用br进行换行 -->
        <br />
        <el-form-item>
          <el-button @click="loginFn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import Mock from 'mockjs'
import Cookie from 'js-cookie'
import { getMenu } from '../../api'

export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      }
    }
  },
  methods: {
    loginFn () {
      // // 使用mock生成token
      // const token = Mock.Random.guid()
      // // 使用cookie存储token
      // Cookie.set('token', token)

      // 校验通过
      this.$refs.form.validate((valid) => {
        if (valid) {
          getMenu(this.form).then((res) => {
            // console.log(res)
            if (res.code === 20000) {
              Cookie.set('token', res.data.token)
              this.$router.push('/home')
            } else {
              this.$message({
                message: '账号密码错误，账号密码都是：admin',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 300px;
  .el-form {
    text-align: center;
  }
}
</style>
